$(document).ready(function () {



var usernametag = document.querySelector(`.top-nav-inner>.top-nav-tool>li:first-child`)
var logouttag = document.querySelector(`.top-nav-inner>.top-nav-tool>li:first-child+li`)
    
    var username = new Utils().getCookie('username')
    if (!username) {
        layer.msg('非法访问！', {
            icon: 2,
            time: 2000
        }, function () {
            localStorage.setItem('url', location.href)
            location.href = 'login.html'
        })
        return false
    } else if (username) {
        console.log(usernametag, logouttag)
        usernametag.innerHTML = `
            欢迎<b>${username}来到唯品会！
        `
        logouttag.innerHTML = `
            <a href="javascript:;" onclick="logout()">退出</a>
        `
    }



    // 发请求获取当前用户的信息
    $.post('/getUser', {
        username
    }, res => {
        res = JSON.parse(res)
        var data = res.data
        console.log(data);
        var str = `



    
        <div class="accountTabs">
            <div class="tab">基本资料</div>
        </div>
        <div class="accountContent">
            <div class="itemsContent">
                <div class="basic-info-form">
                    <div class="forminto clearfix">
                        <div class="login-name">昵称：<label for="">${data.username}</label><span></span></div>
                        <div class="login-tel">手机：<label for="">${data.phone}</label><span></span></div>
                        <div class="login-email">邮箱：<label for="">${data.email}</label><span></span></div>
                        <div class="login-password">密码：<label for="">${data.password}</label><span></span></div>
                        <div class="login-age">年龄：<label for="">${data.age ? data.age : ''}</label><span></span></div>   
                    </div>
                    <div class="btnLayer"><button class="u-button">修改</button></div>
                </div>
            </div>
        </div>`







        $('.account').html(str)
        $('.u-button').click(function () {

            if ($(this).text() === '修改') {

                $('.login-password').find(`label`).html(`<input type="text" name="password"  value="${$('.login-password').find(`label`).text()}">`)
                $('.login-email').find(`label`).html(`<input type="text" name="email" value="${$('.email').find(`label`).text()}">`)
                $('.login-age').find(`label`).html(`<input type="text" name="age" value="${$('.age').find(`label`).text()}">`)
                $('.login-tel').find(`label`).html(`<input type="text" name="phone" value="${$('.phone').find(`label`).text()}">`)
                $(this).text('提交')

            } else if ($(this).text() === '提交') {
                $.post('/updateUser', {
                    password: $('.login-password').children().eq(0).find('input').val(),
                    email: $('.login-email').children().eq(0).find('input').val(),
                    phone: $('.login-tel').children().eq(0).find('input').val(),
                    age: $('.login-age').children().eq(0).find('input').val()

                }, res => {
                    res = JSON.parse(res)
                    console.log(res)
                    if (res.code === 1) {
                        layer.msg('修改成功，请重新登录', {
                            icon: 1,
                            time: 1500
                        }, function () {
                            new Utils().removeCookie('username')
                            localStorage.setItem('url', location.href)
                            location.href = 'login.html'
                        })
                    }
                    // console.log(res);  
                })
            }
        })
    })
    // 修改按钮点击的事件

})